package pers.byxiaobai.levelhealth;

import java.util.Collection;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import pers.byxiaobai.levelhealth.config.ConfigManager;
import pers.byxiaobai.levelhealth.data.PlayerData;
import pers.byxiaobai.levelhealth.data.PlayerDataManager;
import pers.byxiaobai.levelhealth.group.GroupManager;
import pers.byxiaobai.levelhealth.listeners.*;
import pers.byxiaobai.levelhealth.message.MessageManager;
import pers.byxiaobai.levelhealth.runnables.HealthRefreshRunnable;
import pers.byxiaobai.levelhealth.utils.PlayerHealthUtil;

/** 
* @author byxiaobai
* 主类
*/
public class LevelHealth extends JavaPlugin{
	private static LevelHealth plugin;
	public HealthRefreshRunnable healthRefreshRunnable;
	private YamlConfiguration config;
	@Override
	public void onEnable() {
		plugin=this;
		config=ConfigManager.INSTANCE.getConfigYaml("config.yml");
		Bukkit.getPluginManager().registerEvents(new PlayerLevelChangeListener(), this);
		Bukkit.getPluginManager().registerEvents(new PlayerLoginListener(), this);
		healthRefreshRunnable=new HealthRefreshRunnable();
		healthRefreshRunnable.runTaskTimer(this, 1, (int)(config.getDouble("PlayerHealthUpdataTime")*20));
		MessageManager.INSTANCE.init();
		GroupManager.INSTANCE.init();
		getLogger().info("插件加载完毕");
	}
	
	@Override
	public void onDisable() {
		Collection<PlayerData> playerDatas=PlayerDataManager.INSTANCE.getAllPlayerData();
		for(PlayerData playerData:playerDatas) {
			Player player=Bukkit.getPlayer(playerData.playerName);
			PlayerHealthUtil.setPlayerMaxHealth(player, playerData.playerBaseHealth);
		}
		getLogger().info("插件已卸载");
	}
	
	@SuppressWarnings("deprecation")
	public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
	    if (label.equalsIgnoreCase("lh")||label.equalsIgnoreCase("LevelHealth")){
	    	if(sender instanceof Player) {
	    	   Player player = (Player)sender;
	    	   if(args.length==0) {
	    		  if(player.isOp()) {
	    			  player.sendMessage("§a/lh reset 血量 §b重置玩家最大血量(非特殊情况不建议使用)");
	    			  
	    		  }
	    	   }else if(args.length==2) {
	    		   if(args[0].equalsIgnoreCase("reset")) {
	    			   if(!player.isOp()) {
	    				   player.sendMessage("§c你没有权限!");
	    				   return true;
	    			   }
	    			   double maxHealth=Double.parseDouble(args[1]);
	    			   for(Player playerA:Bukkit.getOnlinePlayers()) {
	    				   playerA.setMaxHealth(maxHealth);
	    			   }
	    			   player.sendMessage("§a重置完毕");
	    		   }
	    	   }
	    	   /**
	    	   else if(args.length==1) {
	    		   if(args[0].equalsIgnoreCase("reset")) {
	    			   player.setMaxHealth(20);
	    			   PlayerDataManager.INSTANCE.setIsAddedHealth(player, false);
	    			   player.sendMessage("§a重置完毕");
	    		   }
	    	   }else if(args.length==2) {
	    		   if(args[0].equalsIgnoreCase("add")) {
	    			   double value=Double.parseDouble(args[1]);
	    			   player.setMaxHealth(player.getMaxHealth()+value);
	    		   }
	    	   }
	    	   */
	    	   return true;
	    	}
	    }
	    return false;
	}
	
	public static LevelHealth getPlugin() {
		return plugin;
	}
}
